﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.Products;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using wangyaxin.Models;
using wangyaxin.Wang.Dto;
using wangyaxin.Wyx.Dto;

namespace wangyaxin.Wang
{
    [ApiExplorerSettings(GroupName ="王亚鑫")]
    public class WangAppService:ApplicationService,IWangAppService
    {
        private readonly IRepository<WangModel,int> _WangRepository;

        private readonly IRepository<WangUser,int> _WangUserRepository;

        private readonly IRepository<Product, int> _ProductRepository;

        public WangAppService(IRepository<WangModel,int> WangRepository, IRepository<WangUser, int> WangUserRepository, IRepository<Product, int> ProductRepository)
        {
            _WangRepository = WangRepository;
            _WangUserRepository = WangUserRepository;
            _ProductRepository = ProductRepository;
        }

        [HttpGet,Route("GetWangUser")]
        public async Task<string> GetWangUser(WangUserDTO input)
        {
            var wang = await _WangUserRepository.ToListAsync();
            var str= wang.WhereIf(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name)).ToList();
            var user= ObjectMapper.Map<List<WangUser>, List<WangUserDTO>>(wang);
            return JsonConvert.SerializeObject(new PageDto<WangUserDTO> { code="" , data = user,msg="",count=user.Count});
        }


       

        public async Task<string> AddWangUser(WangUserDTO input)
        {
            var wang = new WangUser
            {
                Name = input.Name,
                Passage = input.Passage,
            };

            await _WangUserRepository.InsertAsync(wang);

            // 将返回格式统一
            var userDto = ObjectMapper.Map<WangUser, WangUserDTO>(wang);
            return JsonConvert.SerializeObject(new PageDto<WangUserDTO>
            {
                code = "", // 可以根据需要设置状态码
                data = new List<WangUserDTO> { userDto }, // 返回添加的用户信息
                msg = "添加成功", // 可以根据需要添加消息
                count = 1 // 添加的用户数量
            });
        }


        [ApiExplorerSettings(GroupName = "张铎")]
        public async Task<WangDto> CreateAsync(CreateWangDto input)
        {
            var wang = new WangModel
            {
                Name = input.Name,
                age = input.age,
            };


            await _WangRepository.InsertAsync(wang);
            return ObjectMapper.Map<WangModel, WangDto>(wang);
        }

        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _WangRepository.DeleteAsync(id);
        }

        public async Task<WangDto> GetAsync(int id)
        {
           var wang=await _WangRepository.GetAsync(id);
            return ObjectMapper.Map<WangModel, WangDto>(wang);
        
        }

     

        public async Task UpdateAsync(int id, UpdateWangDto input)
        {
          var wang=await _WangRepository.GetAsync(id);
            wang.Name = input.Name;
            wang.age = input.age;
            await _WangRepository.UpdateAsync(wang);
        }
    }
}
